﻿<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');
require_once JPATH_COMPONENT.DS.'/helpers/functions.check.php';
require_once JPATH_COMPONENT.DS.'/helpers/functions.moderator.php';
/**
 * HTML View class for the eventcalendar component
 */
class EventcalendarViewEventcalendar extends JView {
	function display($tpl = null) {
		
		//Todo: johonkin järkevämpään paikkaan
		$default_limit = 4;
	
		if (isAdmin())
		{
			//Moderaattorin model
			$moderator_model = &$this->getModel('moderator');
			moderatorData(&$moderator_model, &$this);
		}
		//Saako moderoida parametriksi
		$this->assignRef('moderate', isAdmin());
		
		
		$alkuaika = JRequest::getVar( 'alkuaika' );
				
		if($alkuaika == null)
		{			
			date_default_timezone_set('Europe/Helsinki');
			$alkuaika = date('d.m.Y');
		}
		
		//Monta haetaan
		$limit = JRequest::getVar( 'limit' );
		
		//Oma model
		$model = &$this->getModel();
		if(intval($limit) > 0)
		{
			$events = $model->getEvents($alkuaika, $limit);
		}
		else 
		{
			$events = $model->getEvents($alkuaika);
		}
		
		//Tietty määrä taulukon lopusta, esim kun näytetään lisää tapahtumia
		$kpl = intval(JRequest::getVar( 'kpl',''));
	

		$eventCount = $model->getEventCount($alkuaika);
		if($limit >= count($events))
		{
			//Ei enempää tapahtumia
			$kpl -= $limit - count($events);
		}
		
		if($kpl > 0)
		{
			$events = array_slice($events, -$kpl);
		}
	
		//$events =& $this->get('events');	
		$this->assignRef('events',$events);
		$this->assignRef('alkuaika',$alkuaika);
		$this->assignRef('default_limit',$default_limit);
		$this->assignRef('eventCount',$eventCount);
		parent::display($tpl);
	}

	
}
?>
